# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium import webdriver
import openpyxl
import time
import re

chrome_options = webdriver.FirefoxOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')


def main(name):
    user_list = []
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = "收益"
    sheet.cell(1, 1).value = '用户名'
    sheet.cell(1, 2).value = '昨日币量'
    sheet.cell(1, 3).value = '币价'
    sheet.cell(1, 4).value = '倍率'
    sheet.cell(1, 5).value = '收益'

    with webdriver.Firefox(executable_path='geckodriver.exe', options=chrome_options) as driver:
        with open('user.txt', 'r+', encoding='utf-8') as users:
            for line in users:
                k = line.replace('mining', 'income')
                print(k)
                user_list.append(k)
        for i in user_list:
            try:
                driver.get(i)
                driver.refresh()
                time.sleep(2)
                user_income = []
                header = driver.find_elements_by_class_name("observer-header")[0]
                income = driver.find_elements_by_class_name("item-value")[0]
                username = re.sub(r'-(.*?)-ETH', '', header.text)
                user_income.append(username)
                user_income.append(float(income.text))
                sheet.append(user_income)
                print(user_income)
            except Exception as e:
                print(e)
        wb.save('incomes.xlsx')
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
